'''
Test read_navFile algorithm correctness
'''
#==========================================
#==========================================

#------------------------------------------

from datetime import datetime
import sys
import os
import pandas as pd

import numpy as np
from numpy import dtype

import matplotlib.pyplot as plt




# print(sys.path)

pythonpath=os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
print(pythonpath)
sys.path.insert(0,pythonpath)

import sys
print('Python版本: ',sys.version)

from file.readfile import read_navFile
from funcs.satellite import calSatPos, calSatPos_Batch, calSatPos_Series
from funcs.time import gpst2Utc, utc2Gpst
from funcs.coordinate import xyz2blh
from funcs.plot import plotSatPos

navigationFile='D:\project_git\Calculating_satellite_position\data\BRDC00IGS_R_20213140000_01D_MN.rnx'
# 测试读取文件
print('文件读取')
Nav=read_navFile(navigationFile)
print(Nav.navigation.loc['2021-11-10 00:00:00'].loc['G01'])
# 测试时间转换
print('时间转换')
S=Nav.navigation.loc['2021-11-10 00:00:00'].loc['G01']
time_str=S['epoch']
# print(time_str.second)
gpstime=utc2Gpst(time_str,leapseconds=0)
utctime=gpst2Utc(2183,259200,leapseconds=0)
print(gpstime.week, gpstime.day, gpstime.second, gpstime.ms)
print(utctime)


# 测试计算卫星位置
print('计算卫星位置-calSatPos')
ephemeris_=Nav.navigation.loc['2021-11-10 00:00:00'].loc['G01']
X,Y,Z=calSatPos('G01',ephemeris_.roota,ephemeris_.delta_n,ephemeris_.toe,ephemeris_.epoch,ephemeris_.m0,ephemeris_.eccentricity,ephemeris_.smallomega,
                ephemeris_.cuc,ephemeris_.crc,ephemeris_.cic,ephemeris_.cus,ephemeris_.crs,ephemeris_.cis,ephemeris_.i0,ephemeris_.idot,
                ephemeris_.bigomega0,ephemeris_.bigomegadot)
print(X,Y,Z)

epoch = datetime.strptime('1997-11-9 02:00:00', '%Y-%m-%d %H:%M:%S')
X,Y,Z=calSatPos('G06',5153.65263176,0.451411660250e-8,7200,epoch,-0.290282040486,0.678421219345e-2,-0.258419417299e1,
                0.189989805222e-6,0.201875e3,0.130385169446e-7,0.912137329578e-5,0.40625e1,0.949949026108e-7,
                0.958512160302,-0.253939149013e-9,-0.137835982556e1,-0.819426989566e-8)
print(X,Y,Z)
# 测试批量计算卫星位置
print('计算卫星位置-calSatPos_Batch')
oneSat=Nav.navigation[Nav.navigation['PRN']=='C38']

dt=calSatPos_Batch(oneSat,'C38')


print('计算卫星位置-calSatPos_Series')
dt1=calSatPos_Series(oneSat,'C38')
plotSatPos(dt1)

















# 绘图
# plt.scatter(dt['L'],dt['B'])
# # plt.show()

# print(oneSat.iloc[0])
# sat_pos=[]
# B=[]
# L=[]
# for i in range(0,len(oneSat)):
#     dataline=oneSat.iloc[i]
#     X,Y,Z=calSatPos(dataline.roota,dataline.delta_n,dataline.toe,dataline.epoch,dataline.m0,dataline.eccentricity,dataline.smallomega,
#                     dataline.cuc,dataline.crc,dataline.cic,dataline.cus,dataline.crs,dataline.cis,dataline.i0,dataline.idot,
#                     dataline.bigomega0,dataline.bigomegadot)
    
#     blh=xyz2blh([X,Y,Z])
#     sat_pos.append([X,Y,Z,blh[0],blh[1],blh[2]])
#     B.append(blh[0])
#     L.append(blh[1])



